package keeto.com.vn.classified.ui;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ForgotPassActivity extends Activity implements OnClickListener {
	public static void actionLauch(Context context) {
		Intent i = new Intent(context, ForgotPassActivity.class);
		context.startActivity(i);
	}

	private static final String STATE_KEY_SAVE_EMAIL = "keeto.com.vn.classifeld.ui.ForgotPasswordActivity.Email";
	private static final int FORGOT_PWD_PROGRESS_DIALOG = 0;


	private ImageButton btnBack;
	private ImageButton btnHome;
	private EditText edtUserEmail;
	private Button btn_send;
	private ProgressBar pgBar;
	private ForgotPasswordHandler mHandler = new ForgotPasswordHandler();
	private ForgotPassWordListener mListener= new ForgotPassWordListener();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgot_password_layout);
		initUIComponal();
	}
	private void initUIComponal(){
		btnBack = (ImageButton)findViewById(R.id.btn_title_na_menu);
		btnHome = (ImageButton) findViewById(R.id.btn_title_logo_home);
		edtUserEmail = (EditText)findViewById(R.id.edt_user_or_email);
		btn_send = (Button)findViewById(R.id.btn_send_forgot_pwd);
		pgBar=(ProgressBar)findViewById(R.id.pgbar);

		btnBack.setOnClickListener(this);
		btnHome.setOnClickListener(this);
		btn_send.setOnClickListener(this);

	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_send_forgot_pwd:
			if(!edtUserEmail.getText().toString().equals("")){
				sendRequest();
			}else{
				Toast.makeText(this, getString(R.string.not_allow_emty), Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btn_title_na_menu:
			finish();
			break;
		case R.id.btn_title_logo_home:
			HomeClassifeldActivity.actionLaunch(this);
			break;
		default:
			break;
		}
	}
	private void sendRequest(){
		String email = edtUserEmail.getText().toString();
		ClassifiedCore.getInstance(getApplication()).taskForgotPassWord(mListener, email);
		pgBar.setVisibility(View.VISIBLE);
	}
	class ForgotPasswordHandler extends Handler{

		public void forgotPassWordbyEmail(String email){
			sendRequest();
		}

		
		public void ShowProgressDialog() {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					ForgotPassActivity.this.showDialog(FORGOT_PWD_PROGRESS_DIALOG);
				}
			});
		}

//		public void DismissProgressDialog() {
//			runOnUiThread(new Runnable() {
//				@Override
//				public void run() {
//					ForgotPassActivity.this.dismissDialog(FORGOT_PWD_PROGRESS_DIALOG);
//				}
//			});
//		}
		public void showMsg (final String msg){
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(ForgotPassActivity.this, msg, Toast.LENGTH_SHORT).show();
				}
			});
		}

	}

	class ForgotPassWordListener extends ClassifiedListener{
		@Override
		public void finishSentForgotPassWord(final int returnCode, String detail) {
			// TODO Auto-generated method stub
			super.finishSentForgotPassWord(returnCode, detail);
			runOnUiThread(new Runnable() {
				public void run() {
					pgBar.setVisibility(View.GONE);
					if(returnCode==IProtocol.STATUS_CODE_OK_200){
						Toast.makeText(ForgotPassActivity.this, getString(R.string.text_restore_pass_successful), Toast.LENGTH_SHORT).show();
					} else if(returnCode==IProtocol.STATUS_CODE_ACCOUNT_ALREADY_EXIST_672){
						Toast.makeText(ForgotPassActivity.this, getString(R.string.text_restore_pass_failed), Toast.LENGTH_SHORT).show();
						
					}
				}
			});
//			mHandler.DismissProgressDialog();
//			if (detail != null && !detail.equals("")){
//				mHandler.showMsg(detail);
//			}
		}
	}

}
